// pages/personal/personal.js
import request from '../../utils/request'
let startY=0;
let moveY=0;
let moveDistance =0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
      coverTransform: 'translateY(0)',
      coverTransition: '', //变化回弹平滑移动
      userInfo: {},
      recordPlayList: [],//播放记录
      type: 1 ,//week:1,allData:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    let uid
    if(userInfo){
      var userInfoJson = JSON.parse(userInfo);
      console.log('本地存储：',userInfoJson)
      uid = userInfoJson.userId;
      console.log('用户id:',uid)
      this.setData({
        userInfo: userInfoJson
      })
      //获取用户播放记录
      this.getUserRecordList(uid,this.data.type);
    }
  },
  //避免在生命周期函数添加 async
  async getUserRecordList(uid,type){
    let recordPlayListData = await request('/user/record',{uid,type})
    let index = 0;
    let recordPlayListTemp;
    //type =1 周数据，type =0 所有数据，100条
    if(type === 1){
      recordPlayListTemp = recordPlayListData.weekData.splice(0,10);
    }else if(type === 0 ){
      recordPlayListTemp = recordPlayListData.allData.splice(0,10);
    }
    let recordPlayList = recordPlayListTemp.map((item)=>{
      item.id = index++;
      return item;
    }) 
    this.setData({
      recordPlayList
    })
    console.log("获取的播放信息：",recordPlayList);
  },
  // 手指点击
  handleTouchStart(event){
    this.setData({
      coverTransition: ''
    })
    // 获取手指的起始坐标
    startY = event.touches[0].clientY;
  },
  //手指移动
  handleTouchMove(event){
    moveY = event.touches[0].clientY;
    moveDistance = moveY - startY;

    if(moveDistance <0){
      return;
    }else if(moveDistance >=80){
        moveDistance = 80;
    }
    this.setData({
      coverTransform: `translateY(${moveDistance}px)`,
    })
  },
  // 手指抬起
  handleTouchEnd(){
    //恢复到默认的位置，类似回弹效果
    this.setData({
      coverTransform: `translateY(0)`,
      coverTransition: 'transform 1s linear'
    })
  },
  //登录接口
  toLogin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})